<?php
include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Input.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/21
 * Time: 10:51
 */
class InputDAO{
    const FIELD_ID = "inputId";
    const FIELD_ACTION_ID = "actionId";
    const FIELD_TYPE = "inputType";
    const FIELD_VALUE = "inputValue";
    const FIELD_DESCRIPTION = "inputDesc";

    public $conn;

    function __construct()
    {
        $this->conn = new db();
    }

    //插入输入
    public function insert($input){

    }

    //删除输入
    public function delete($inputId){

    }

    //查询输入
    public function query($inputId){
        $sql = "select * from actioninput where ".self::FIELD_ID." = ".$inputId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $input = $this->loadDataFromResult($row);
            return $input;
        }
        else
            return null;
    }

    //更新输入
    public function update($input){

    }

    //查询一个行为的所有输入
    public function queryInputListByAction($actionId){
        $sql = "select * from actioninput where ".self::FIELD_ACTION_ID." = ".$actionId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){			
        	$inputs = array();;
            foreach ($rows as $row){
                $input = $this->loadDataFromResult($row);
                array_push($inputs,$input);
            }
            return $inputs;
        }
        else
            return null;
    }

    //从结果集中构建出input对象
    private function loadDataFromResult($row){
        $input = new Input();
        $input->setId($row[self::FIELD_ID]);
        $input->setActionId($row[self::FIELD_ACTION_ID]);
        $input->setDesc($row[self::FIELD_DESCRIPTION]);
        $input->setValue($row[self::FIELD_VALUE]);
        $input->setType($row[self::FIELD_TYPE]);
        return $input;
    }
}
